﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using YahooYQL.Classes;

namespace YahooYQL.Forms
{
    #region Enums Type
    /// <summary>
    /// Result Type in XML or JSON
    /// </summary>
    public enum ResultType
    {
        XML,
        JSON
    }
    #endregion
    
    public partial class YQLConsole : Form
    {
        #region Properties  
        /// <summary>
        /// Selected Radio.
        /// </summary>
        private int SelectedTypeIndex;
        #endregion
               
        #region Events
        private void YQLConsole_Load(object sender, EventArgs e)
        {
            this.SetDefaulValue();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            this.DownloadString();
        }

        private void rbtResultType_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton selectedRbt = (RadioButton)sender;
            switch (selectedRbt.Name.ToLower())
            {
                case "rbtxml":
                    this.SelectedTypeIndex = 0;
                    break;
                case "rbtjson":
                    this.SelectedTypeIndex = 1;
                    break;
                default:
                    this.SelectedTypeIndex = 0;
                    break;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Default Constructor.
        /// </summary>
        public YQLConsole()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// Set Default Value.
        /// </summary>
        private void SetDefaulValue()
        {
            this.txtYQLStatement.Text = "select * from geo.countries where place=\"North America\"";
            this.rbtXML.Checked = true;
            this.ckbDebug.Checked = false;
            this.ckbDiagnostics.Checked = true;
            this.SelectedTypeIndex = 0; // XML
        }

        /// <summary>
        /// Get Result.
        /// </summary>
        private void DownloadString()
        {            
            StringBuilder theWebAddress = new StringBuilder();
            theWebAddress.Append(PublicName.YahooQueryUrl);
            theWebAddress.Append("q=" + System.Web.HttpUtility.UrlEncode(this.txtYQLStatement.Text.Trim()));
            theWebAddress.Append("&format=" + Enum.GetName(typeof(ResultType), this.SelectedTypeIndex));
            theWebAddress.Append("&diagnotics=" + this.ckbDiagnostics.Checked.ToString());
            
            string result = string.Empty;
            using (WebClient wc = new WebClient())
            {
                this.Cursor = Cursors.WaitCursor;
                result = wc.DownloadString(theWebAddress.ToString());
                this.wbrResult.DocumentText = result;
                this.Cursor = Cursors.Default;
            }
        }
        #endregion       
    }
}
